#pragma once

#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class WebServer {
    int sockfd;
    sockaddr_in socketAddr;

    const static int MAX_CLIENTS = 32;
    const static int PORT_NUM = 5011;  // 学号后4位

    static void *threadStartRoutine(void *sfd);  // for WebServer only
    static void threadRoutine(int sfd);  // for WebServer only

    static void handleRequest(const std::string &request, std::string &response);
    static void handleGet(const std::string &path, std::string &response);
    static void handlePost(const std::string &path, const std::string &args, std::string &response);

    static void router(const std::string &requestPath, std::string &realPath);

    static void generateResponsePacket(
        const std::string &data, 
        const int code, 
        std::string &response
    );
    static void generateResponsePacket(
        const std::string &data, 
        const int code, 
        const std::string &contentType,
        std::string &response
    );

public: 
    WebServer();
    ~WebServer();

    void run();
};
